/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.inter.AbstractActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * 贸易任务
 * 
 * @author beedoorwei
 * 
 */
public class StockTask extends TaskUnit
{

	public StockTask()
	{
		this.username = "贸易任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			Iterator<String> ite = UserCache.userQueue.keySet().iterator();
			while (ite.hasNext())
			{
				String username = ite.next();
				TaskManager.addTask(new Stock(username));
			}
		} catch (Exception e)
		{
			LoggerUtil.error(e);
		}
	}

	/**
	 * 贸易任务
	 * 
	 * @author Administrator
	 * 
	 */
	class Stock extends TaskUnit
	{
		public Stock(String username)
		{
			this.username = username;
		}

		public void doTask()
		{
			try
			{
				if (isInStockTime())
				{
					UserData u = UserCache.get(username);
					if (u.getUserState() == UserState.LOGIN_GAME)
					{
						HttpUtil.doGet(username, GameConstants.STOCK_QUERY_URL, new PreStockAction(username));
					} else
					{
						// 添加登录任务先
						TaskManager.addTask(new LoginTask(username, this));
					}
				}
			} catch (Exception e)
			{
				LoggerUtil.error(e);
			}
		}
	}

	/**
	 * 是否是贸易时间
	 * 
	 * @return
	 */
	private boolean isInStockTime()
	{
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hour >= 8 && hour <= 23)
		{
			return true;
		}
		return false;
	}

	/**
	 * 贸易前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class PreStockAction extends AbstractActionAfterHttpRequest
	{
		public PreStockAction(String username)
		{
			super();
			this.username = username;
			this.taskUnit = new Stock(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData stockInfo)
		{
			// 目前只做苹果和铁矿生意，别的不做
			int currentpositon = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.currentpositon"));
			int postion = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.postion"));
			int cd = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.cd"));// cd
			int cantrade = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.cantrade"));// 能否贸易
			int g2Prize = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g2.prize"));// 苹果价格
			int g4Prize = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g4.prize"));// 铁矿价格
			int g2cd = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g2.cd"));// 苹果cd
			int g4cd = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g4.cd"));// 铁矿cd
			int g2num = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g2.num"));// 苹果num
			int g4num = GameUtil.convertInt(XmlDataUtil.getNodeData(stockInfo, "results.g4.num"));// 铁矿num

			if (cd > 0)
			{
				// 休眠指定cd后再来贸易
				LoggerUtil.info(UserCache.getGameName(username) + "贸易中，休眠 " + GameUtil.getRemainTime(cd + ""));
				TaskManager.addTask(this.taskUnit, cd / 1000 + 1);
				return;
			}

			if (g2Prize > 143)
			{
				// 开始卖苹果,先计算卖多少次
				int times = currentpositon / g2num + ((currentpositon % g2num) > 0 ? 1 : 0);
				int time = times * g2cd * 60;
				doStock(times, 1, 2, time);
			} else if (g4Prize > 194)
			{
				// 开始卖铁矿,先计算卖多少次
				int times = currentpositon / g4num + ((currentpositon % g4num) > 0 ? 1 : 0);
				int time = times * g4cd * 60;
				doStock(times, 1, 2, time);
			} else if (g2Prize < 80)
			{
				// 买入苹果，先计算买多少次
				int needpositon = postion - currentpositon;
				int times = needpositon / g2num + ((needpositon % g2num) > 0 ? 1 : 0);
				int time = times * g2cd * 60;
				doStock(times, 0, 2, time);
			} else if (g4Prize < 110)
			{
				// 买入铁矿,先计算买多少次
				int needpositon = postion - currentpositon;
				int times = needpositon / g4num + ((needpositon % g4num) > 0 ? 1 : 0);
				int time = times * g4cd * 60;
				doStock(times, 0, 4, time);
			}
		}

		/**
		 * @param num
		 * @param tradeType
		 * @param goodId
		 * @param time
		 */
		public void doStock(int num, int tradeType, int goodId, int time)
		{
			List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
			nvpList.add(new NameValuePair("num", num + ""));
			nvpList.add(new NameValuePair("tradeType", tradeType + ""));
			nvpList.add(new NameValuePair("goodId", goodId + ""));

			LoggerUtil.info(UserCache.getGameName(username) + getTradeType(tradeType) + getGoodName(goodId) + num + "次");
			HttpUtil.doPost(username, GameConstants.STOCK_TRADE_URL, nvpList, new StockResultAction(username, time));
		}
	}

	/**
	 * @param goodId
	 * @return
	 */
	private String getGoodName(int goodId)
	{
		switch (goodId)
		{
		case 1:
			return "绿豆";
		case 2:
			return "苹果";
		case 3:
			return "木材";
		case 4:
			return "铁矿";
		case 5:
			return "黄金";
		case 6:
			return "珍珠";
		case 7:
			return "钻石";
		}
		return "";
	}

	private String getTradeType(int t)
	{
		if (1 == t)
		{
			return "卖出";
		} else
		{
			return "买入";
		}

	}

	class StockResultAction extends AbstractActionAfterHttpRequest
	{
		int time = 0;

		public StockResultAction(String username, int time)
		{
			super();
			this.username = username;
			this.taskUnit = new Stock(username);
			this.time = time;
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData stockInfo)
		{
			LoggerUtil.info(username + "贸易执行成功");
			TaskManager.addTask(taskUnit, 30 * 60);

		}
	}

}
